/**
 * Data Cleaners - 数据清洗工具集
 * 统一导出入口
 */

const DateTimeCleaner = require('./DateTimeCleaner');
const CurrencyCleaner = require('./CurrencyCleaner');
const IDCardCleaner = require('./IDCardCleaner');
const PhoneCleaner = require('./PhoneCleaner');
const AddressCleaner = require('./AddressCleaner');

/**
 * 根据字段类型自动选择清洗器
 * @param {string} fieldType - 字段类型
 * @returns {Object|null} 对应的清洗器
 */
function getCleanerForType(fieldType) {
    const cleanerMap = {
        'datetime': DateTimeCleaner,
        'date': DateTimeCleaner,
        'time': DateTimeCleaner,
        'timestamp': DateTimeCleaner,
        'currency': CurrencyCleaner,
        'money': CurrencyCleaner,
        'amount': CurrencyCleaner,
        'decimal': CurrencyCleaner,
        'idcard': IDCardCleaner,
        'id_card': IDCardCleaner,
        'identity': IDCardCleaner,
        'phone': PhoneCleaner,
        'mobile': PhoneCleaner,
        'tel': PhoneCleaner,
        'address': AddressCleaner,
        'location': AddressCleaner,
        'province': AddressCleaner,
        'city': AddressCleaner
    };

    return cleanerMap[fieldType?.toLowerCase()] || null;
}

/**
 * 批量清洗数据
 * @param {Object[]} rows - 数据行数组
 * @param {Object} fieldConfigs - 字段配置 { fieldName: 'type' }
 * @returns {Object[]} 清洗后的数据
 */
function cleanRows(rows, fieldConfigs) {
    return rows.map(row => {
        const cleanedRow = { ...row };

        for (const [field, type] of Object.entries(fieldConfigs)) {
            if (cleanedRow[field] !== undefined && cleanedRow[field] !== null) {
                const cleaner = getCleanerForType(type);
                if (cleaner) {
                    const cleaned = cleaner.clean(cleanedRow[field]);
                    if (cleaned !== null) {
                        cleanedRow[field] = cleaned;
                    }
                }
            }
        }

        return cleanedRow;
    });
}

/**
 * 根据字段名自动推断清洗器类型
 * @param {string} fieldName - 字段名
 * @returns {string|null} 推断的类型
 */
function inferCleanerType(fieldName) {
    const name = fieldName.toLowerCase();

    // 日期时间类
    if (name.includes('time') || name.includes('date') || name.includes('日期') || name.includes('时间')) {
        return 'datetime';
    }

    // 金额类
    if (name.includes('amount') || name.includes('money') || name.includes('金额') ||
        name.includes('balance') || name.includes('余额') || name.includes('price') ||
        name.includes('价格') || name.includes('费用') || name.includes('收入') || name.includes('支出')) {
        return 'currency';
    }

    // 身份证类
    if (name.includes('idcard') || name.includes('id_card') || name.includes('身份证') ||
        name.includes('identity') || name.includes('证件号')) {
        return 'idcard';
    }

    // 电话类
    if (name.includes('phone') || name.includes('mobile') || name.includes('电话') ||
        name.includes('手机') || name.includes('contact')) {
        return 'phone';
    }

    // 地址类
    if (name.includes('address') || name.includes('location') || name.includes('地址') ||
        name.includes('住址') || name.includes('省') || name.includes('市') || name.includes('区')) {
        return 'address';
    }

    return null;
}

module.exports = {
    DateTimeCleaner,
    CurrencyCleaner,
    IDCardCleaner,
    PhoneCleaner,
    AddressCleaner,
    getCleanerForType,
    cleanRows,
    inferCleanerType
};
